﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Carbon.Web.Forms.Collections
{
    public class ListDelegate<T> : IList
    {

         private IList<T> m_OriginalCollection= default(IList<T>);
					public IList<T> OriginalCollection
					{
						get
						{
							return m_OriginalCollection;
						}
					} 

        private Func<T, bool> m_WhereDelegate = default(Func<T, bool>);
        public Func<T, bool> WhereDelegate
        {
            get
            {
                return m_WhereDelegate;
            }
        }

        public ListDelegate(IList<T> originalCollection, Func<T, bool> whereDelegate)
            : base()
        {
            if (originalCollection == null)  throw new NullReferenceException("originalCollection");
            if (whereDelegate == null) throw new ArgumentNullException("whereDelegate");

            this.m_OriginalCollection = originalCollection;
            this.m_WhereDelegate = whereDelegate;
        }


        public void Remove(object o)
        {
            OriginalCollection.Remove((T)o);
        }

        public void Add(object o)
        {
            OriginalCollection.Add((T)o);
        }

        public System.Collections.IEnumerator GetEnumerator()
        {
            return OriginalCollection.Where(i => WhereDelegate(i)).GetEnumerator();
        }

        int IList.Add(object value)
        {
             if ((WhereDelegate((T)value) != false) == false)  throw new InvalidOperationException("(WhereDelegate((T)value) != false) == false");
             OriginalCollection.Add((T)value);
             return OriginalCollection.Where(WhereDelegate).Count();
        }

        public void Clear()
        {
            OriginalCollection.Clear();
        }

        public bool Contains(object value)
        {
            return OriginalCollection.Where(WhereDelegate).Contains((T)value);
        }

        public int IndexOf(object value)
        {
            throw new NotImplementedException();
        }

        public void Insert(int index, object value)
        {
            throw new NotImplementedException();
        }

        public bool IsFixedSize
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsReadOnly
        {
            get { throw new NotImplementedException(); }
        }

        public void RemoveAt(int index)
        {
            throw new NotImplementedException();
        }

        public object this[int index]
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public void CopyTo(Array array, int index)
        {
            throw new NotImplementedException();
        }

        public int Count
        {
            get { return OriginalCollection.Where(WhereDelegate).Count(); }
        }

        public bool IsSynchronized
        {
            get { return false; }
        }

        public object SyncRoot
        {
            get { throw new NotImplementedException(); }
        }
    }
}
